import { parseToObject } from '@/utils';
import { CELLULAR_CLASSIFY_LIST, CELLULAR_PRIORITY_LIST } from '@/constants';
import { INITIAL_FORM_QUERY_VALUE } from '../../constants';
import { formatDate } from '@/utils/util';

export default {
  name: 'cellular-area-config-header-form',
  props: {
    topChannelList: {
      type: Array,
      default: () => [],
    },
    // 表单默认值
    defaultFormQueryValueStr: String,
    querying: {
      type: Boolean,
      default: false,
    },
    cellularList: {
      type: Array,
      default: () => [],
    },
    // 省市区
    cityList: {
      type: Array,
      default: () => [],
    },
  },

  data() {
    return {
      formQuery: { ...INITIAL_FORM_QUERY_VALUE },
      topChannelMap: null,
      labelWidth: '80px',
      okBtnLoading: false,
      createTime: [],
      cellularClassifyList: CELLULAR_CLASSIFY_LIST,
      cellularPriorityList: [...CELLULAR_PRIORITY_LIST],
      basicWidth: '80px',
    };
  },
  watch: {
    // 监听传入的表单默认值变化
    defaultFormQueryValueStr(newValue) {
      if (!newValue) {
        return;
      }
      this.formQuery = {
        ...this.formQuery,
        ...parseToObject(newValue),
      };
    },
  },
  methods: {
    onQuery() {
      this.$emit('onQuery', {
        ...this.formQuery,
        city: this.formQuery.city && this.formQuery.city.length > 0 ? this.formQuery.city[this.formQuery.city.length - 1] : undefined,
      });
    },
    reset() {
      this.formQuery = { ...INITIAL_FORM_QUERY_VALUE };
      this.createTime = [];
    },
    timeChange(time) {
      if (time) {
        this.createTime = [formatDate(time[0], 'yyyy-MM-dd hh:mm:ss'), formatDate(time[1], 'yyyy-MM-dd hh:mm:ss')];
        this.formQuery = { ...this.formQuery, timeRange: { startTime: this.createTime[0], endTime: this.createTime[1] } };
      } else {
        this.createTime = [];
        this.formQuery = { ...this.formQuery, timeRange: null };
      }
    },
  },
  async mounted() {},
};
